package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api("菜品管理")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    //redis
    @Autowired
    private RedisTemplate redisTemplate; //用于缓存菜品信息
    @ApiOperation("新增菜品")
    @PostMapping
    public Result startOrStop(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        //保存菜品
        dishService.save(dishDTO);

        //清理缓存

        //设置redis的key，使用分类dish_id作为key
        String key = "dish_" + dishDTO.getCategoryId();
        //因为在缓存的时候是将list存进去的，新增后缓存内没有数据，所以要清理缓存，之后重新缓存
        cleanCache("key");
        log.info("清除缓存:{}",key);
        return Result.success();
    }

    //菜品查询，分页
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){

        log.info("菜品查询，分页");
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    //批量删除和单个删除
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam String ids) {
        log.info("批量删除菜品：{}", ids);
        if (ids == null ) {
            return Result.error("没有ids");
        }
        //将ids转换为数组
        String[] split = ids.split(",");
        //将数组转换为list
        List<Long> idsList = new ArrayList<>();
        for (String id : split) {
            idsList.add(Long.valueOf(id));
        }
        dishService.delete(idsList);

        //清理缓存,删除所有菜品缓存数据,以dish_开头的key，自定义下方
        cleanCache("dish_*");
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.update(dishDTO);

        //清理缓存,删除所有菜品缓存数据,以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }
    //根据id查询菜品
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishvo = dishService.getById(id);
        return Result.success(dishvo);
    }
    //修改菜品状态
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("修改菜品状态:{}{}",status,id);
        dishService.updataStatus(status,id);

        //清理缓存,删除所有菜品缓存数据,以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    //根据分类id查询菜品
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}
